package cn.walls1717.juc.future;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;

/**
 * thenCombine的使用
 *
 * @author walls1717
 * @create 2022/12/09 17:09
 **/
@Slf4j
public class CompletableFutureCombineDemo {

    public static void main(String[] args) {
        CompletableFuture<Integer> futureA = CompletableFuture.supplyAsync(() -> {
            log.info(Thread.currentThread().getName() + "线程启动");
            return 10;
        });

        CompletableFuture<Integer> futureB = CompletableFuture.supplyAsync(() -> {
            log.info(Thread.currentThread().getName() + "线程启动");
            return 20;
        });

        // thenCombine会处理两个future的结果，先获得结果的会等待另一个。
        CompletableFuture<Integer> result = futureA.thenCombine(futureB, (x, y) -> {
            log.info("处理两个结果");
            return x + y;
        });

        log.info(String.valueOf(result.join()));
    }
}
